﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using Avalonia.Platform;
using BQDHelper.ViewModels;
using BQDHelper.Views;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
using MsBox.Avalonia.Models;
using System;
using System.Collections.Generic;

namespace BQDHelper;

public partial class App : Application
{

    private static TrayIcon? NotifyIcon;
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        // Line below is needed to remove Avalonia data validation.
        // Without this line you will get duplicate validations from both Avalonia and CT
        BindingPlugins.DataValidators.RemoveAt(0);

        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            desktop.MainWindow = new MainWindow
            {
                DataContext = new MainViewModel()
            };
        }
        else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
        {
            singleViewPlatform.MainView = new MainView
            {
                DataContext = new MainViewModel()
            };
        }

        //创建托盘图标
        InitTrayIcon();

        base.OnFrameworkInitializationCompleted();
    }

    /// <summary>
    /// 初始化托盘图标
    /// </summary>
    private void InitTrayIcon()
    {
        var ShowMainWindow = new NativeMenuItem("显示窗口");
        //var importDBMenuItem = new NativeMenuItem("导入数据库");
        //var exportDBMenuItem = new NativeMenuItem("导出数据库");
        var SeparatorMenuItem = new NativeMenuItemSeparator();
        var ExitMenuItem = new NativeMenuItem("退出");
        ShowMainWindow.Click += ShowWindow;
        ExitMenuItem.Click += ExitApplication;
        NotifyIcon = new TrayIcon
        {
            ToolTipText = "安装助手",
            Icon = new WindowIcon(AssetLoader.Open(new Uri("avares://BQDHelper/Assets/avalonia-logo.ico"))),
            Menu = [ShowMainWindow, SeparatorMenuItem, ExitMenuItem]
        };
        NotifyIcon.Clicked += ShowWindow;
    }

    /// <summary>
    /// 退出程序按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void ExitApplication(object? sender, System.EventArgs e)
    {
        var result = await MessageBoxManager.GetMessageBoxStandard(
            "提示",
            "您确定要退出安装助手吗",
            ButtonEnum.YesNo,
            Icon.Question
            ).ShowAsync();
        if (result == ButtonResult.No)
            return;
        //先隐藏后退出防止图标残留
        if (NotifyIcon != null)
        {
            NotifyIcon.IsVisible = false;
        }
        Environment.Exit(0);
    }

    /// <summary>
    /// 显示主窗口
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ShowWindow(object? sender, System.EventArgs e)
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop && desktop.MainWindow != null)
        {
            desktop.MainWindow.Hide();
            desktop.MainWindow.Show();
            if (desktop.MainWindow.WindowState == WindowState.Minimized)
            {
                desktop.MainWindow.Activate();
            }
            //显示主窗口
            desktop.MainWindow.Topmost = true;
            desktop.MainWindow.Topmost = false;
        }
    }
}
